import { request_post, request_get, request_put, request_delete, request_file } from 'services/common/request';
import pathToRegexp from 'path-to-regexp';

let toPathGroup = pathToRegexp.compile('/api/data_articles/groups/:id');
/**
 * 获取运动列表
 * @param {Object} data
 * @param {String} data.pageStart
 * @param {String} data.pageOffset
 * @param {String} data.pageNum
 */
export function getList(data = {}) {
	return request_get('/sport/sportList', data);
}

/**
 * 上传图片
 * @param {Object} data
 * @param {String} data.file
 */
export function getTask(data = {}) {
	return request_file('/upload/uploadTempfile', { file: data.file });
}

/**
 * 获取菜单权限列表
 * @param {Object} data
 */
export function authority(data = {}) {
	return request_get('/user/userPermissionList', data);
}

/**
 * 获取菜单运动列表
 * @param {Object} data
 */
export function menuSport(data = {}) {
	return request_get('/menu/struct', data);
}

/**
 * 获取首页信息列表
 * @param {Object} data
 */
export async function getHomeInfos(data = {}) {
	const { data: persion } = await request_get('/sport/humanNumBar', data); // 运动人数
	const { trenInfo, trendData } = await getHomeTrend(data); // 运动人数趋势
	const { data: count } = await request_get('/sport/outline', data); // 统计图
	const { totalData, totalInfo } = await request_get('/sport/totalData', data).then((res) => {
		const { 区域运动人次占比: total, ...info } = res.data;
		if (total) {
			return {
				totalData: filterTotalData(total),
				totalInfo: info
			};
		} else {
			return {
				totalData: [],
				totalInfo: info
			};
		}
	}); // 总累计客流量
	count.saturability = (count.saturability * 100).toFixed(2);
	return {
		persion: persion || {},
		trend: trendData || [],
		trenInfo: trenInfo || {},
		totalData: totalData || [],
		totalInfo: totalInfo || [],
		count
	};
}

function filterTotalData(data) {
	let arr = [];
	for (const key in data) {
		if (data.hasOwnProperty(key)) {
			const num = data[key];
			arr.push({ value: num, name: key });
		}
	}
	return arr;
}

/**
 * 运动人数趋势
 * @param {Object} data
 */
export async function getHomeTrend(data = {}) {
	return request_get('/sport/homeHumanNumTrend', data).then((res) => {
		return {
			trenInfo: res.data,
			trendData: res.data.trendData
		};
	}); // 运动人数趋势
}

/**
 * 获取首页信息列表
 * @param {Object} data
 */
export async function getHomeInfo(data = {}) {
	const { data: persion } = await request_get('/sport/humanNumBar', data); // 运动人数
	const { data: trend } = await request_get('/sport/humanNumTrend', data); // 趋势图
	const { data: count } = await request_get('/sport/outline', data); // 统计图
	count.saturability = (count.saturability * 100).toFixed(2);
	return {
		persion: persion || {},
		trend: trend || {},
		count
	};
}

/**
 * 获取客流详情
 * @param {Object} data
 */
export async function getKeliuInfo(data = {}) {
	const { data: people } = await request_get('/sport/compareTrend', data); // 运动人数和人次
	const { data: saturation } = await request_get('/sport/baoheduAndJiliuli', data); // 集流力
	return {
		people: people || {},
		saturation: saturation || {}
	};
}

/**
 * 获取客流详情全部
 * @param {Object} data
 */
export async function getKeliuInfos(data = {}) {
	return await request_get('/sport/passengerFlow', data).then(({ data }) => {
		let obj = {};
		let arr = { 运动人数: 'personNumSum', 运动人次: 'entNumSum', 饱和度: 'baoHeDu', 集流力: 'jiLiuLi' };
		for (const key in arr) {
			let objs = {};
			for (const keys in data) {
				objs[keys] = data[keys][arr[key]];
			}
			obj[key] = objs;
		}
		return obj;
	}); // 运动人数和人次
}

/**
 * 获取天气详情
 * @param {Object} data
 */
export async function getWeatherInfo(data = {}) {
	const { data: dayNum } = await request_get('/weather/weatherDayNum', data); // 各种气候天数
	const { data: HumanNumAvg } = await request_get('/weather/weatherHumanNumAvg', data); // 日均
	let wenduTrend = await getAnalysisByType(data);
	return {
		dayNum,
		HumanNumAvg,
		wenduTrend
	};
}

export async function getAnalysisByType(data) {
	let wenduTrend;
	switch (data.ctype) {
		case '0':
			await request_get('/weather/wenduTrend', data).then((res) => {
				wenduTrend = res.data;
			}); // 温度区间
			break;
		case '1':
			await request_get('/weather/shiduTrend', data).then((res) => {
				wenduTrend = res.data;
			}); // 湿度区间
			break;
		case '2':
			await request_get('/weather/qualityTrend', data).then((res) => {
				wenduTrend = res.data;
			}); // pm2.5区间
			break;
		default:
			break;
	}
	return runOne(wenduTrend, data.ctype);
}

/**
 * 获取对比分析运动人次
 * @param {Object} data
 */
export async function getAnalysisRc(data = {}) {
	const { data: dayNum } = await request_get('/sport/compareTrend', data);
	return dayNum;
}
/**
 * 获取对比分析饱和度和集流力
 * @param {Object} data
 */
export async function getAnalysisJL(data = {}) {
	const { data: dayNum } = await request_get('/sport/baoheduAndJiliuli', data);
	return dayNum;
}

function runOne(data, type) {
	let obj = {};
	switch (type) {
		case '0':
			obj = runderAll();
			obj = {
				'0以下': [],
				...obj,
				'40以上': []
			};
			for (let key in data) {
				if (key <= 0) {
					obj['0以下'] = [ ...obj['0以下'], data[key] ];
					continue;
				}
				if (key > 40) {
					obj['40以上'] = [ ...obj['40以上'], data[key] ];
					continue;
				}
				let k = runTwo(key);
				obj[k] = [ ...obj[k], data[key] ];
			}
			break;
		case '1':
			obj = runderAll(10, 10, '%');
			obj = {
				'1-10%': [],
				...obj,
				'100%以上': []
			};
			for (let key in data) {
				if (key <= 0) {
					obj['1-10%'] = [ ...obj['1-10%'], data[key] ];
					continue;
				}
				if (key > 100) {
					obj['100%以上'] = [ ...obj['100%以上'], data[key] ];
					continue;
				}
				let k = runTwo(key, 10, 10, '%');
				obj[k] = [ ...obj[k], data[key] ];
			}
			break;
		case '2':
			return data;
			break;
		default:
			break;
	}
	return average(obj);
}

function runTwo(i, k = 5, max = 8, t = '') {
	for (let n = 1; n <= max; n++) {
		if (i >= k * n - (k - 1) && i <= k * n) {
			return `${k * n - (k - 1)}-${k * n}${t}`;
		}
	}
}

function runderAll(k = 5, max = 8, t = '') {
	let obj = {};
	for (let n = 1; n <= max; n++) {
		obj[`${k * n - (k - 1)}-${k * n}${t}`] = [];
	}
	return obj;
}

function average(obj) {
	let newObj = {};
	for (let key in obj) {
		if (obj[key].length) {
			newObj[key] = obj[key].reduce((a, b) => a + b) / obj[key].length;
		} else {
			newObj[key] = 0;
		}
	}
	return newObj;
}
